package com.jishaokang.service.impl;

import com.jishaokang.base.Result;
import com.jishaokang.cache.ResultCache;
import com.jishaokang.dao.TeacherDAO;
import com.jishaokang.dao.UserDAO;
import com.jishaokang.model.dto.Teacher;
import com.jishaokang.model.dto.User;
import com.jishaokang.model.vo.UserVO;
import com.jishaokang.service.TeacherService;
import com.jishaokang.util.ForbidUtil;
import com.jishaokang.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.List;


/**
 * Created by NANA_Final on 2019/6/21.
 */
@Service
public class TeacherServiceImpl implements TeacherService {

    @Autowired
    private TeacherDAO teacherDAO;

    @Autowired
    private TokenUtil tokenUtil;

    @Autowired
    private ForbidUtil forbidUtil;

    @Override
    public Result addTeacher(Teacher teacher) throws ParseException {
        int userId = tokenUtil.getUserIdByToken(teacher.getToken());
        teacher.setUserId(userId);
        Teacher teacherInDB = teacherDAO.selectByUserId(userId);
        if (teacherInDB!=null) return ResultCache.getDataError(401,"您已成为讲师，不能重复申请");
        forbidUtil.updateUserPower(teacher.getUserId(),forbidUtil.SET_TEACHER);
        teacherDAO.insert(teacher);
        return ResultCache.OK;
    }

    @Override
    public Result selectTeacherByUserId(Teacher teacher) {
        int userId = tokenUtil.getUserIdByToken(teacher.getToken());
        teacher.setUserId(userId);
        Teacher teacher1 = teacherDAO.selectByUserId(userId);
        return ResultCache.getDataOk(teacher1);
    }

    @Override
    public Result updateTeacher(Teacher teacher) {
        int userId = tokenUtil.getUserIdByToken(teacher.getToken());
        teacher.setUserId(userId);

        Teacher teacher1 = teacherDAO.selectByUserId(userId);
        if (teacher.getCareer() == null) teacher.setCareer(teacher1.getCareer());
        if (teacher.getTeacherIntroduction() == null) teacher.setTeacherIntroduction(teacher1.getTeacherIntroduction());
        if (teacher.getTeacherName() == null) teacher.setTeacherName(teacher1.getTeacherName());
        teacher.setTeacherId(teacher1.getTeacherId());
        System.out.println(teacher);
        teacherDAO.update(teacher);
        return ResultCache.OK;
    }

    @Override
    public Result selectTeacher(Teacher teacher) {
        int total = teacherDAO.selectTotal(teacher);
        System.out.println(teacher);
        teacher.setPage((teacher.getPage()-1)*teacher.getNumber());
        List<Teacher> teachers = teacherDAO.select(teacher);
        return ResultCache.getListDataOk(total,"teachers",teachers);
    }

    @Override
    public Result selectTeacherByTeacherNameVague(Teacher teacher) {
        teacher.setTeacherName("%"+teacher.getTeacherName()+"%");
        int total = teacherDAO.selectTotalByTeacherNameVague();
        teacher.setPage((teacher.getPage()-1)*teacher.getNumber());
        List<Teacher> teachers = teacherDAO.selectByTeacherNameVague();
        return ResultCache.getListDataOk(total,"teachers",teachers);
    }

    @Override
    public Result updateTeacherCostPercent(Teacher teacher) {
        teacherDAO.updateCostPercent(teacher);
        return ResultCache.OK;
    }
}